package blog

import (
	"context"
	"time"
	"vblog/common"
)

type Service interface {
	CreateBlog(context.Context, *CreateBlogRequest) (*Blog, error)
	QueryBlog(context.Context, *QueryBlogRequest) (*BlogSet, error)
	UpdateBlog(context.Context, *UpdateBlogRequest) (*Blog, error)
	DescribeBlog(context.Context, *DescribeBlogRequest) (*Blog, error)
	DeleteBlog(context.Context, *DeleteBlogRequest) (*Blog, error)
	UpdateBlogStatus(context.Context, *UpdateBlogStatusRequest) (*Blog, error)
}

type (
	QueryBlogRequest struct {
		*common.PageRequest
		Keywords string `json:"keywords"`
	}
	UpdateBlogRequest struct {
		BlogId             int                `json:"blog_id,omitempty"`
		UpdateMode         common.UPDATE_MODE `json:"update_mode,omitempty"`
		*CreateBlogRequest `validate:"required"`
	}

	DescribeBlogRequest struct {
		BlogId int `json:"blog_id"`
	}
	DeleteBlogRequest struct {
		BlogId int `json:"blog_id"`
	}

	UpdateBlogStatusRequest struct {
		BlogId int `json:"blog_id"`
		*ChangeBlogStatusRequest
	}
)

func NewDescribeBlogRequest(blogId int) *DescribeBlogRequest {
	return &DescribeBlogRequest{
		BlogId: blogId,
	}
}

func NewDeleteBlogRequest(blogId int) *DeleteBlogRequest {
	return &DeleteBlogRequest{
		BlogId: blogId,
	}
}

func NewQueryBlogRequest() *QueryBlogRequest {
	return &QueryBlogRequest{
		PageRequest: common.NewPageRequest(),
	}
}

func NewUpdateBlogRequest(blogId int) *UpdateBlogRequest {
	return &UpdateBlogRequest{
		BlogId: blogId,
		// 默认增量更新
		UpdateMode:        common.UPDATE_MODE_PATCH,
		CreateBlogRequest: NewCreateBlogRequest(),
	}
}

func NewUpdateBlogStatusRequest(blogId int) *UpdateBlogStatusRequest {
	return &UpdateBlogStatusRequest{
		BlogId:                  blogId,
		ChangeBlogStatusRequest: NewChangeBlogStatusRequest(),
	}
}

func (u *UpdateBlogStatusRequest) UpdateStatus(s STATUS) {
	u.Status = s
	if u.Status == STATUS_PUBLISHED {
		u.PublishedAt = time.Now().Unix()
	}
}
